package com.cjq.auth.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.cjq.auth.exception.ServiceException;
import com.cjq.auth.remote.SysUserRemoteService;
import com.cjq.auth.service.AuthService;
import com.cjq.common.constants.JwtConstants;
import com.cjq.common.constants.TokenConstants;
import com.cjq.common.domain.SysUser;
import com.cjq.common.domain.request.SysUserReq;
import com.cjq.common.domain.response.SysUserRes;
import com.cjq.common.result.Result;
import com.cjq.common.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    private SysUserRemoteService sysUserRemoteService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;
    @Override
    public Result<SysUserRes> accountLogin(SysUserReq sysUserReq) {
        if (StrUtil.isBlank(sysUserReq.getUserName())){
            throw new ServiceException("账号不能为空");
        }
        if (StrUtil.isBlank(sysUserReq.getUserPwd())){
            throw new ServiceException("密码不能为空");
        }
        Result<SysUser> user = sysUserRemoteService.findUser(sysUserReq.getUserName());
        SysUser data = user.getData();
        if (data==null){
            throw new ServiceException("用户不存在，去注册");
        }
        if (!data.getUserPwd().equals(sysUserReq.getUserPwd())){
            throw new ServiceException("密码错误");
        }
        HashMap<String, Object> map = new HashMap<>();
        String userKey = UUID.randomUUID().toString().replaceAll("-", "");
        map.put(JwtConstants.USER_KEY,userKey);
        String token = JwtUtils.createToken(map);
        SysUserRes sysUserRes = new SysUserRes();
        sysUserRes.setToken(token);
        sysUserRes.setTime(TokenConstants.TOKEN_EXPIRATION_TIME_STR);
        redisTemplate.opsForValue().set(TokenConstants.LOGIN_TOKEN_KEY+userKey, JSONObject.toJSONString(data),15, TimeUnit.MINUTES);
        return Result.success(sysUserRes);
    }

    @Override
    public Result<SysUser> userInfo() {
        //请求头里拿token
        String token = request.getHeader(TokenConstants.TOKEN);
        //反向解析userKey
        String userKey = JwtUtils.getUserKey(token);
        //取缓存
        String userJSONStr = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        return Result.success(JSONObject.parseObject(userJSONStr,SysUser.class));
    }

    @Override
    public Result logout() {
        //请求头里拿token
        String token = request.getHeader(TokenConstants.TOKEN);
        //反向解析userKey
        String userKey = JwtUtils.getUserKey(token);
        //删缓存
        redisTemplate.delete(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        return Result.success();
    }
}
